package com.cat.model.web.action.admin.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alonew.core.commons.page.AjaxObject;
import com.alonew.core.commons.page.Page;
import com.cat.model.biz.logic.SystemLogic;
import com.cat.model.biz.service.admin.SystemService;
import com.cat.model.common.model.admin.AdminRoleVO;
import com.cat.model.common.model.admin.AdminUserRoleVO;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.dao.mapper.ModelMapper;
import com.cat.model.web.action.base.AdminAction;

public class AdminUserRoleManagerAction extends AdminAction{
	
	@Autowired
	private PlmCommDAO plmCommDAO;
	
	@Autowired
	private SystemService systemService;
	
	@Autowired
	private SystemLogic systemLogic;
	
	@Autowired
	private ModelMapper modelMapper;
	
	private static final long serialVersionUID = 425636316597986569L;
	
	private AdminRoleVO adminRole;
	private Long id;
	private String userId;
	private String roleId;
	private String description;
	
	/**
	 * list
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String list() {
		Map<String, Object> condition = new HashMap<String, Object>();
		
		if(StringUtils.isNotEmpty(userId)){
			condition.put("userId", userId);
		}
		
		Page page = this.fetchPageParams();
		page.setNumPerPage(20);
		
		Long totalCount = plmCommDAO.searchSize("manager.searchAdminRoleSize", condition);
		page.setTotalCount(totalCount);
		int start = (page.getPageNum() -1) * page.getNumPerPage();
		if(start < 0){
			start = 0;
		}
		
		List<AdminRoleVO> list = (List<AdminRoleVO>)plmCommDAO.search("manager.searchAdminRole", condition, start, page.getNumPerPage());
		
		request.setAttribute("list", list);
		request.setAttribute("size", totalCount);
		
		Page page2 = this.fetchPageParams();
		page2.setNumPerPage(20);
		
		Long totalCount2 = plmCommDAO.searchSize("manager.searchAdminUserRoleSize", condition);
		page2.setTotalCount(totalCount2);
		int start2 = (page2.getPageNum() -1) * page2.getNumPerPage();
		if(start2 < 0){
			start2 = 0;
		}
		
		List<AdminUserRoleVO> list2 = (List<AdminUserRoleVO>)plmCommDAO.search("manager.searchAdminUserRole", condition, start, page.getNumPerPage());
		
		request.setAttribute("list2", list2);
		request.setAttribute("size2", totalCount2);
		
		return "userRolelist";
	}
	
	public String addUserRole(){
		try { 
			AdminUserRoleVO userRole = modelMapper.getAdminUserRole(new Long(userId),new Long(roleId));
			if(userRole != null){
				this.print(AjaxObject.newError("该用户已拥有该角色").setCallbackType("").toString());
				return "userRolelist";
			}
			userRole = new AdminUserRoleVO();
			userRole.setRoleId(new Long(roleId));
			userRole.setUserId(new Long(userId));
			userRole.setDescription(description);
			systemService.insertAdminUserRole(userRole);
		} catch (Exception e) {
			e.printStackTrace();
			this.print(AjaxObject.newError(e.getMessage()).setCallbackType("").toString());
			return "userRolelist";
		}
		this.print(AjaxObject.newOk("新增成功！").setCallbackType("").toString());
		return "userRolelist";
	}
	
	public String deleteSingleRole() {
		try {
			systemLogic.deleteAdminUserRole(id);
		} catch (Exception e) {
			e.printStackTrace();
			this.print(AjaxObject.newError(e.getMessage()).setCallbackType("").toString());
			return "userRolelist";
		}
		this.print(AjaxObject.newOk("删除成功！").setCallbackType("").toString());
		return "userRolelist";
	}


	public AdminRoleVO getAdminRole() {
		return adminRole;
	}

	public void setAdminRole(AdminRoleVO adminRole) {
		this.adminRole = adminRole;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

}
